package cn.bluemobi.platform.controller.merchant;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bluemobi.common.core.dto.MapDto;
import cn.bluemobi.common.core.dto.PageCondition;
import cn.bluemobi.platform.core.controller.PlatformBaseController;
import cn.bluemobi.platform.core.dto.DatatablePage;
import cn.bluemobi.platform.entity.member.Merchant;
import cn.bluemobi.platform.service.MerchantService;

/**
 * 商户管理controller
 * 
 * @author qifnagke
 * 
 */
@Controller
@RequestMapping("/admin/merchant")
public class MerchantController extends PlatformBaseController {

	@Autowired
	private MerchantService merchantService;

	@RequestMapping("/toMerchantPage")
	public String toMerchantPage(ModelMap model) {
		return "merchant/merchantList";
	}

	@RequestMapping("/findMerchants")
	@ResponseBody
	public DatatablePage findMerchants(PageCondition cond) {
		return new DatatablePage(cond, merchantService.findMerchants(cond,
				getUser()));
	}

	@RequestMapping("/toMerchantDetailPage")
	public String toMerchantDetailPage(
			@RequestParam(value = "merchantId", required = false) String merchantId,
			ModelMap model) {
		if (!StringUtils.isEmpty(merchantId)) {
			Merchant merchant = merchantService.findMerchantDetail(merchantId);
			model.put("merchant", merchant);
		}
		return "merchant/merchantDetail";
	}

	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public MapDto saveOrUpdate(Merchant merchant) {
		return merchantService.saveOrUpdate(merchant, getUser());
	}

	@RequestMapping("/deleteMerchant")
	@ResponseBody
	public MapDto deleteMerchant(@RequestParam("merchantId") String merchantId) {
		return merchantService.deleteMerchant(merchantId, getUser());
	}
}
